//
//  RouteDetailViewController.m
//  TravelFirst
//
//  Created by lanou3g on 15/9/22.
//  Copyright (c) 2015年 吴娅君. All rights reserved.
//

#import "RouteDetailViewController.h"
#import "RouteDetailView.h"
#import "RouteDetailModel.h"
#import "RouteCommentModel.h"
#import "RoutealbumModel.h"
@interface RouteDetailViewController ()<UIScrollViewDelegate>
//接收轮播图图片的数组
@property (nonatomic,strong) NSMutableArray *CycleArr;
@property(nonatomic,strong) NSMutableArray *ImgArr;
@property(nonatomic,strong)RouteDetailView* views;
@property(nonatomic,strong)RouteDetailModel *model;
@property(nonatomic,strong)NSMutableArray *kwordarr;
@property(nonatomic,strong)NSMutableArray *commentArr;
@property(nonatomic,strong)NSMutableArray *actArr;
@property(nonatomic,strong)NSMutableArray *albumArr;
@property(nonatomic,strong)NSMutableArray *xgArr;
@end

@implementation RouteDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.tabBarController.tabBar.alpha = 0;

    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.translucent=NO;
    [self JSONData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSMutableArray *)xgArr{
    if (!_xgArr) {
        self.xgArr=[NSMutableArray array];
    }
    return _xgArr;
}
-(NSMutableArray *)albumArr{
    if (!_albumArr) {
        self.albumArr=[NSMutableArray array];
    }
    return _albumArr;
}
-(NSMutableArray *)actArr{
    if (!_actArr) {
        self.actArr=[NSMutableArray array];
    }
    return _actArr;
}
-(NSMutableArray *)commentArr{
    if (!_commentArr) {
        self.commentArr=[NSMutableArray array];
    }
    return _commentArr;
}
-(NSMutableArray *)ImgArr{
    if (!_ImgArr) {
        self.ImgArr=[NSMutableArray array];
    }
    return _ImgArr;
}
-(NSMutableArray *)kwordarr{
    if (!_kwordarr) {
        self.kwordarr=[NSMutableArray array];
    }
    return _kwordarr;
}
-(NSMutableArray *)CycleArr{
    if (!_CycleArr) {
        self.CycleArr=[NSMutableArray array];
    }
    return _CycleArr;
}
-(void)JSONData{
    [LORequestManger GET:self.detailurl success:^(id response) {
        NSDictionary *dic=(NSDictionary *)response;
        for (NSDictionary * val in dic[@"pictures"]) {
            RouteDetailModel *model= [RouteDetailModel new];
            [model setValuesForKeysWithDictionary:val];
            [self.CycleArr addObject:model];
        }
        for (NSDictionary *v in dic[@"traits"]) {
            [self.ImgArr addObject:v[@"url"]];
        }
        _model= [RouteDetailModel new];
        [_model setValuesForKeysWithDictionary:dic];
        self.kwordarr=dic[@"crowds"];
        [self jsonComment];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
//解析评论数据
-(void)jsonComment{
    [LORequestManger GET:self.commentUrl success:^(id response) {
        NSDictionary * dic=(NSDictionary *)response;
        for (NSDictionary *val in dic[@"comments"]) {
            RouteCommentModel *model=[RouteCommentModel new];
            [model setValuesForKeysWithDictionary:val];
            [self.commentArr addObject:model];
        }
        
        if([self.actUrl isEqualToString:@""]){
            [self JSONzj];
        }else{
            [self Jsonact];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
//解析近期活动数据
-(void)Jsonact{
    [LORequestManger GET:self.actUrl success:^(id response) {
        NSDictionary * dic=(NSDictionary *)response;
        for (NSDictionary *val in dic[@"activities"]) {
            RouteCommentModel *model=[RouteCommentModel new];
            [model setValuesForKeysWithDictionary:val];
            [self.actArr addObject:model];
        }
        [self JSONzj];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
//解析专辑数据
-(void)JSONzj{
    [LORequestManger GET:self.zjUrl success:^(id response) {
        NSDictionary * dic=(NSDictionary *)response;
        for (NSDictionary *val in dic[@"logs"]) {
            RoutealbumModel *model=[RoutealbumModel new];
            [model setValuesForKeysWithDictionary:val];
            [self.albumArr addObject:model];
        }
        [self JSONxglx];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
//解析相关路线数据
-(void)JSONxglx{
    [LORequestManger GET:self.xgUrl success:^(id response) {
        NSDictionary * dic=(NSDictionary *)response;
        for (NSDictionary *val in dic[@"trails"]) {
            [self.xgArr addObject:val];
        }
        [self SetUpCycleView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
}
//适配
-(CGFloat)setframe:(NSString *)str width:(CGFloat)width fontsize:(NSInteger)size{
    CGRect ss=[str boundingRectWithSize:CGSizeMake(width, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:size] forKey:NSFontAttributeName] context:nil];
    return ss.size.height;
}
//设置视图
- (void)SetUpCycleView{
    CGFloat h=[self setframe:self.model.detail width:kWidth-20 fontsize:13];
    RouteCommentModel *model1=self.commentArr[0];
    RouteCommentModel *model2=self.commentArr[1];
    CGFloat commentone=[self setframe:model1.comment width:kWidth-30 fontsize:12];
    CGFloat commenttwo=[self setframe:model2.comment width:kWidth-30 fontsize:12];
    self.views= [[RouteDetailView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight) imageArray:self.CycleArr delegate:self action:@selector(TouchImage) timer:3.5 selector:@selector(button) Height:h num:self.kwordarr.count commentone:commentone commenttwo:commenttwo acttotal:self.actArr.count albumImgArr:self.albumArr albumaction:@selector(albumTouchImage) albumtimer:3.5 albumselector:@selector(albumbutton) lastArr:self.xgArr];
    [self.view addSubview:self.views];
    [self setVal];
}

//设置值
-(void)setVal{
    [self.views setIMG:self.ImgArr];
    [self.views setVal:self.model];
    [self.views people:self.kwordarr];
    RouteCommentModel *model1=self.commentArr[0];
    RouteCommentModel *model2=self.commentArr[1];
    [self.views setCommentVal:self.total_star modelone:model1 modeltwo:model2];
    if(self.actArr.count!=0){
        [self.views setAct:self.actArr];
    }
    
}

//轮播图的点击方法
- (void)TouchImage{
    //起始位置为下标1,我们第一张显示的图片前面还有一张图片 所以我们需要将下标-1 从0开始
    int index = self.views.scr.contentOffset.x/kWidth;
    index -= 1;
    
    //当我们的index为最后一张的时候 我们将index的值设为0 以便于我们可以根据下标进行相对操作(页面跳转 去数组内某一下标的值等)
    if(index == self.CycleArr.count){
        index = 0;
    }
    
}

//轮播图定时器的方法  设置循环滚动
- (void)button{
    CGPoint newPoint = self.views.scr.contentOffset;
    newPoint.x += kWidth;
    [self.views.scr setContentOffset:newPoint animated:YES];
    [self scrollAndButton];
}

//封装循环滚动的方法  在手动滑动与定时器方法内调用
- (void)scrollAndButton{
    //如果当前偏移量为0时表示
    if(self.views.scr.contentOffset.x == 0){
        [self.views.scr setContentOffset:CGPointMake( self.views.scr.contentSize.width - (2 * self.views.scr.bounds.size.width), 0) animated:NO];
    }else if(self.views.scr.contentOffset.x == self.views.scr.contentSize.width - self.views.scr.bounds.size.width){
        [self.views.scr setContentOffset:CGPointMake(self.views.scr.bounds.size.width, 0) animated:NO];
    }
}



//精选游记轮播图的点击方法
- (void)albumTouchImage{
    //起始位置为下标1,我们第一张显示的图片前面还有一张图片 所以我们需要将下标-1 从0开始
    int index = self.views.albumscr.contentOffset.x/(kWidth-20);
    index -= 1;
    
    //当我们的index为最后一张的时候 我们将index的值设为0 以便于我们可以根据下标进行相对操作(页面跳转 去数组内某一下标的值等)
    if(index == self.albumArr.count){
        index = 0;
    }
    NSLog(@"%d",index);
}

//精选游记轮播图定时器的方法  设置循环滚动
- (void)albumbutton{
    CGPoint newPoint = self.views.albumscr.contentOffset;
    newPoint.x += (kWidth-20);
    [self.views.albumscr setContentOffset:newPoint animated:YES];
    [self albumscrollAndButton];
}

//精选游记封装循环滚动的方法  在手动滑动与定时器方法内调用
- (void)albumscrollAndButton{
    //如果当前偏移量为0时表示
    if(self.views.albumscr.contentOffset.x == 0){
        [self.views.albumscr setContentOffset:CGPointMake( self.views.albumscr.contentSize.width - (2 * self.views.albumscr.bounds.size.width), 0) animated:NO];
    }else if(self.views.albumscr.contentOffset.x == self.views.albumscr.contentSize.width - self.views.albumscr.bounds.size.width){
        [self.views.albumscr setContentOffset:CGPointMake(self.views.albumscr.bounds.size.width, 0) animated:NO];
    }
}

@end
